package com.example.evaluate.service;

import com.example.evaluate.mapper.*;
import com.example.evaluate.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassroomService implements IClassroomService{
    @Autowired
    ClassroomMapper classroomMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    TeacherCourseMapper teacherCourseMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StudentTeacherMapper studentTeacherMapper;

    /**
     * 通过classroomId找到一个班级的teacher集合，用于添加学生
     *
     * @param classroomId
     */
    @Override
    public List<Teacher> findAllTeacherByClassroomId(int classroomId) {
        return classroomMapper.findAllTeacherByClassroomId(classroomId);
    }

    /**
     * addCourse
     * <p>
     * 添加teacher_course表，classroom_teacher表
     *
     * @param classroomId
     * @param trueTeacherId
     * @param teacherBatch
     * @param teacherDepartment
     * @param teacherCourse
     * @param teacherCourseClassification
     * @param teacherParticipant
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCourse(String classroomName,int classroomId, int trueTeacherId, String teacherBatch, String teacherDepartment, String teacherCourse, String teacherCourseClassification, String teacherParticipant) {
        //先获取老师信息
        TrueTeacher trueTeacher = teacherMapper.findTrueTeacherByTeacherId(trueTeacherId);

        //增加teacher course
        teacherCourseMapper.addTeacherCourse(teacherCourse,trueTeacherId);

        //增加classroom teacher
        ClassRoom classRoom = new ClassRoom(classroomId,trueTeacher.getTrueTeacherName(),teacherBatch,teacherDepartment,teacherCourse,teacherCourseClassification,teacherParticipant);
        classroomMapper.addClassroomTeacher(classRoom);

        //为班级学生挨个添加新的课程
        Teacher temp = new Teacher(trueTeacher.getTrueTeacherName(),teacherBatch,teacherDepartment,teacherCourse,teacherCourseClassification,teacherParticipant);

        //通过班级获取学生信息
        List<Student> students = studentMapper.findStudentByStudentClass(classroomName);
        for (Student s:
           students  ) {
            teacherMapper.addTeacher(temp);
            TeacherAndStudent teacherAndStudent = new TeacherAndStudent(s.getStudentId(),temp.getTeacherId());

            int res = studentTeacherMapper.addStudentTeacher(teacherAndStudent);
            if (res<0)
                return false;
        }

        return true;
    }


}
